<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>校园失物招领</title>
    <link rel="stylesheet" href="../lib/bootstrap.min.css">
    <link rel="stylesheet" href="../css/main.css">
    <link rel="stylesheet" href="../layui/css/layui.css">

</head>

<body>

<div class="navbar-default" style="height:25px;">
    <div class="container">
    </div>
</div>

<div class="container" style="margin-top: 10px;">
    <div class="col-sm-2" id="sidebar">
        <div class="list-group side-bar hidden-xs" id="channel">
            <a href="#" class="list-group-item active" id="findAll">综合</a>
            <a href="#" class="list-group-item" id="findLost">失物</a>
            <a href="#" class="list-group-item" id="findLoster">拾物</a>
            <a href="#" class="list-group-item" id="noticeList">公告</a>
        </div>
    </div>
    <div class="col-sm-7" id="PushInfoData">
        <ul class="flow-default" style="height: 300px;" id="newsList">
        </ul>
    </div>

    <div class="col-sm-3">
        <div class="input-group">
            <input type="text" id="title" class="form-control" onkeydown="onKeyDown(event)"/>
            <span class="input-group-btn">
                    <button class="btn btn-info btn-search" onclick="findPushInfo()" style="margin-left:0px">查找</button>
                </span>
        </div>
        <div class="side-bar-card flag clearfix" id="user">
            <div id="userData">
                <ul class="nav navbar-nav" style="margin-left: 30%;">
                    <li><a href="login.html">登录</a></li>
                    <li><a href="register.html">注册</a></li>
                </ul>
            </div>
        </div>
        <div class="side-bar-card flag clearfix">
            <div class="col-xs-5">
                <img src="../img/img007.png">
            </div>
            <div class="col-xs-7">
                <div class="text-lg">有害信息举报专区</div>
                <div>举报电话: 12377</div>
            </div>
        </div>
    </div>
</div>
<div class="footer">

</div>
</body>
<script src="../lib/jquery-3.3.1.js"></script>
<script src="../js/application.js"></script>
<script src="../layui/layui.js"></script>
<script src="../lib/jquery.params.js"></script>
<script>
    var userId = '0';
    var phone = '0'
    var noticeListClick = function () {
        $('#channel a').removeClass("active");
        $(this).addClass("active");
        layui.use(['layer'], function () {
            $ = layui.$
                , layer = layui.layer
            $.ajax({
                url: urlapi + 'notice/selectAll',
                data: {},
                type: "Post",
                dataType: "json",
                success: function (data) {
                    $("#newsList").html("")
                    $.each(data.data, function (index, item) {
                        $("#newsList").append(
                            '<div class="news-list-item clearfix">' +
                            '<div class="">' +
                            '<a href="notice.html?noticeId=' + item.noticeId + '" class="title text-center" style="font-size:22px">  ' + item.title + '</a>' +
                            '<a href="notice.html?noticeId=' + item.noticeId + '" class="noticeListContent" > ' + item.content + '</a>' +
                            '<div class="info">' +
                            '<span>' + item.admin.nickName + '</span> ·' +
                            '<span>' + item.createdDate + '</span> ·' +
                            '</div></div></div>'
                        );

                    });
                },
                error: function () {
                    layer.msg('公告查询失败');
                }
            });
        })
    }

    var findAll = function () {
        var lastType = -1;
        var id = $(this).attr("id");
        if (id != undefined && id != null && id != "") {
            $('#channel a').removeClass("active");
            $(this).addClass("active");
            switch (id) {
                case "findAll":
                    lastType = -1;
                    break;
                case "findLost":
                    lastType = 0;
                    break;
                case "findLoster":
                    lastType = 1;
                    break;
            }
        }
        $("#newsList").html("")
        layui.use('flow', function () {
            var flow = layui.flow;
            var pages = 1;
            flow.load({
                elem: '#newsList' //流加载容器
                , isAuto: false
                , isLazyimg: true
                , done: function (page, next) { //加载下一页
                    //模拟插入
                    setTimeout(function () {
                        $.ajax({
                            url: urlapi + "pushInfo/obtainPushInfo?status=0&lastType=" + lastType + "&page=" + pages + "&limit=10",
                            type: "get",
                            dataType: "json",
                            success: function (data) {
                                var pushinfistr = data.data;
                                var pushinfolist = JSON.parse(pushinfistr);
                                var lis = [];
                                for (var i = 0; i < pushinfolist.length; i++) {
                                    var imglist = findUploadImg(pushinfolist[i].content);
                                    if (imglist == null) {
                                        lis.push('<li class="news-noimg-list-item clearfix" style="list-style-type:none"><div class="col-xs-12"><a href="#" class="title">' +
                                            pushinfolist[i].title + '</a><div class="info"><span class="avator"></span><span>' +
                                            pushinfolist[i].user.nickName + '</span> ·<span>' + pushinfolist[i].likeNumber + '点赞</span> ·<span>' +
                                            pushinfolist[i].createdDate + '</span> ·</div></div></li>')
                                    } else {
                                        lis.push('<li class="news-list-item clearfix" style="list-style-type:none"><div class="col-xs-5"><img src='
                                            + urlapi + '/upload/' + pushinfolist[i].user.phone + '/' + imglist[0] +
                                            ' alt="" height="90px"></div><div class="col-xs-7"><a href="#" class="title">' +
                                            pushinfolist[i].title + '</a><div class="info"><span class="avator"></span><span>' +
                                            pushinfolist[i].user.nickName + '</span> ·<span>' + pushinfolist[i].likeNumber + '点赞</span> ·<span>' +
                                            pushinfolist[i].createdDate + '</span> ·</div></div></li>')
                                    }
                                }
                                next(lis.join(''), page < data.count/10); //假设总页数为 6
                            },
                            error: function () {
                                alert("数据请求失败！");
                            }
                        });
                        pages++;
                    }, 500);
                }

            });
        });
    }

    var updateUser = function () {
        window.open('updateUser.html');
    }
    var quit = function () {
        var r = window.confirm("确定退出?");
        if (r) {
            layui.use(['layer'], function () {
                $ = layui.$
                    , layer = layui.layer
                showloading(true)
                $.ajax({
                    url: urlapi + 'user/quit',
                    data: {},
                    type: "Post",
                    dataType: "json",
                    success: function (data) {
                        if (data.code == 1) {
                            layer.msg("退出成功");
                            setTimeout(function () {
                                $(location).attr('href', 'index.html');
                            }, 1000);
                        }
                    },
                    error: function () {

                    }
                });
            })
        }
    }
    $("#noticeList").on('click', noticeListClick)
    $("#findAll").on('click', findAll)
    $("#findLost").on('click', findAll)
    $("#findLoster").on('click', findAll)

    //回车触发搜索
    function onKeyDown(event){
        var e = event || window.event || arguments.callee.caller.arguments[0];
        if(e && e.keyCode==13){
            findPushInfo();
        }
    }

    //点击触发搜索
    function findPushInfo() {
        $("#newsList").html("");
        layui.use('flow', function () {
            var flow = layui.flow;
            var pages = 1;
            var pageSize = 10;
            var content = $('#title').val();
            flow.load({
                elem: '#newsList' //流加载容器
                , isAuto: false
                , isLazyimg: true
                , done: function (page, next) { //加载下一页
                    //模拟插入
                    setTimeout(function () {
                        $.ajax({
                            url: urlapi + "pushInfo/getByContent",
                            data: {
                                page: page,
                                limit: pageSize,
                                content: content
                            },
                            type: "get",
                            dataType: "json",
                            success: function (data) {
                                var pushinfolist = data.data;
                                if (pushinfolist.count === 0){
                                    alert("没有找到呀!");
                                    findAll;
                                    return;
                                }
                                var lis = [];
                                for (var i = 0; i < pushinfolist.length; i++) {
                                    var imglist = findUploadImg(pushinfolist[i].content);
                                    if (imglist == null) {
                                        lis.push('<li class="news-noimg-list-item clearfix" style="list-style-type:none"><div class="col-xs-12"><a href="#" class="title">' +
                                            pushinfolist[i].title + '</a><div class="info"><span class="avator"></span><span>' +
                                            pushinfolist[i].user.nickName + '</span> ·<span>' + pushinfolist[i].likeNumber + '点赞</span> ·<span>' +
                                            pushinfolist[i].createdDate + '</span> ·</div></div></li>')
                                    } else {
                                        lis.push('<li class="news-list-item clearfix" style="list-style-type:none"><div class="col-xs-5"><img src='
                                            + urlapi + '/upload/' + pushinfolist[i].user.phone + '/' + imglist[0] +
                                            ' alt="" height="90px"></div><div class="col-xs-7"><a href="#" class="title">' +
                                            pushinfolist[i].title + '</a><div class="info"><span class="avator"></span><span>' +
                                            pushinfolist[i].user.nickName + '</span> ·<span>' + pushinfolist[i].likeNumber + '点赞</span> ·<span>' +
                                            pushinfolist[i].createdDate + '</span> ·</div></div></li>')
                                    }
                                }
                                next(lis.join(''), page < data.count / pageSize);
                            },
                            error: function () {
                                alert("数据请求失败！");
                            }
                        });
                        pages++;
                    }, 500);
                }

            });
        });
    }

    //我的收藏
    function myLove(userId) {
        $("#newsList").html("");
        layui.use('flow', function () {
            var flow = layui.flow;
            var pages = 1;
            var pageSize = 10;
            var content = $('#title').val();
            flow.load({
                elem: '#newsList' //流加载容器
                , isAuto: false
                , isLazyimg: true
                , done: function (page, next) { //加载下一页
                    //模拟插入
                    setTimeout(function () {
                        $.ajax({
                            url: urlapi+"collect/getCollectByUser",
                            data: {
                                page: page,  //页数
                                limit: pageSize,  //每页几条
                                userId: userId, //用户ID
                            },
                            type: "get",
                            dataType: "json",
                            success: function (data) {
                                var pushinfolist = data.data;
                                if (pushinfolist.count === 0){
                                    alert("没有找到呀!");
                                    findAll;
                                    return;
                                }
                                var lis = [];
                                for (var i = 0; i < pushinfolist.length; i++) {
                                    var imglist = findUploadImg(pushinfolist[i].content);
                                    if (imglist == null) {
                                        lis.push('<li class="news-noimg-list-item clearfix" style="list-style-type:none"><div class="col-xs-12"><a href="#" class="title">' +
                                            pushinfolist[i].title + '</a><div class="info"><span class="avator"></span><span>' +
                                            pushinfolist[i].user.nickName + '</span> ·<span>' + pushinfolist[i].likeNumber + '点赞</span> ·<span>' +
                                            pushinfolist[i].createdDate + '</span> ·</div></div></li>')
                                    } else {
                                        lis.push('<li class="news-list-item clearfix" style="list-style-type:none"><div class="col-xs-5"><img src='
                                            + urlapi + '/upload/' + pushinfolist[i].user.phone + '/' + imglist[0] +
                                            ' alt="" height="90px"></div><div class="col-xs-7"><a href="#" class="title">' +
                                            pushinfolist[i].title + '</a><div class="info"><span class="avator"></span><span>' +
                                            pushinfolist[i].user.nickName + '</span> ·<span>' + pushinfolist[i].likeNumber + '点赞</span> ·<span>' +
                                            pushinfolist[i].createdDate + '</span> ·</div></div></li>')
                                    }
                                }
                                next(lis.join(''), page < data.count / pageSize);
                            },
                            error: function () {
                                alert("数据请求失败！");
                            }
                        });
                        pages++;
                    }, 500);
                }

            });
        });
    }

    //我的发布
    function myPush(userId) {
        $("#newsList").html("");
        layui.use('flow', function () {
            var flow = layui.flow;
            var pages = 1;
            var pageSize = 10;
            var content = $('#title').val();
            flow.load({
                elem: '#newsList' //流加载容器
                , isAuto: false
                , isLazyimg: true
                , done: function (page, next) { //加载下一页
                    //模拟插入
                    setTimeout(function () {
                        $.ajax({
                            url: urlapi+"pushInfo/getUserPushInfo",
                            data: {
                                page: page,
                                limit: pageSize,
                                userId: userId,
                            },
                            type: "get",
                            dataType: "json",
                            success: function (data) {
                                var pushinfolist = data.data;
                                if (pushinfolist.count === 0){
                                    alert("没有找到呀!");
                                    findAll;
                                    return;
                                }
                                var lis = [];
                                for (var i = 0; i < pushinfolist.length; i++) {
                                    var imglist = findUploadImg(pushinfolist[i].content);
                                    if (imglist == null) {
                                        lis.push('<li class="news-noimg-list-item clearfix" style="list-style-type:none"><div class="col-xs-12"><a href="#" class="title">' +
                                            pushinfolist[i].title + '</a><div class="info"><span class="avator"></span><span>' +
                                            pushinfolist[i].user.nickName + '</span> ·<span>' + pushinfolist[i].likeNumber + '点赞</span> ·<span>' +
                                            pushinfolist[i].createdDate + '</span> ·</div></div></li>')
                                    } else {
                                        lis.push('<li class="news-list-item clearfix" style="list-style-type:none"><div class="col-xs-5"><img src='
                                            + urlapi + '/upload/' + pushinfolist[i].user.phone + '/' + imglist[0] +
                                            ' alt="" height="90px"></div><div class="col-xs-7"><a href="#" class="title">' +
                                            pushinfolist[i].title + '</a><div class="info"><span class="avator"></span><span>' +
                                            pushinfolist[i].user.nickName + '</span> ·<span>' + pushinfolist[i].likeNumber + '点赞</span> ·<span>' +
                                            pushinfolist[i].createdDate + '</span> ·</div></div></li>')
                                    }
                                }
                                next(lis.join(''), page < data.count / pageSize);
                            },
                            error: function () {
                                alert("数据请求失败！");
                            }
                        });
                        pages++;
                    }, 500);
                }

            });
        });
    }

    $(function () {
        //侧栏跟随
        var $sidebar = $("#sidebar"),
            $window = $(window),
            offset = $sidebar.offset(),
            topPadding = 15;

        $window.scroll(function () {
            if ($window.scrollTop() > offset.top) {
                $sidebar.stop().animate({
                    marginTop: $window.scrollTop() - offset.top + topPadding
                });
            } else {
                $sidebar.stop().animate({
                    marginTop: 0
                });
            }
        });

        var type = GetQueryString("type");
        $('#channel a').removeClass("active");
        switch (type) {
            case "findAll":
                $("#findAll").addClass("active");
                findAll();
                break;
            case "findLost":
                $("#findLost").addClass("active");
                break;
            case "findLoster":
                $("#findLoster").addClass("active");
                break;
            case "noticeList":
                $("#noticeList").addClass("active");
                noticeListClick();
                break;
            default:
                $("#findAll").addClass("active");
                break;
        }

        $.ajax({
            url: urlapi + 'user/isLogin',
            data: {},
            type: "Post",
            dataType: "json",
            // < ! --加上此部分 Start-- >
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            // < ! --加上此部分 End-- >
            success: function (data) {

                if (data.code == -9999) {
                    return
                }
                $("#user").html("")
                var userData = data.data;
                userId = userData.userId;
                phone = userData.phone;
                var htmlL = '<div><div class="col-xs-5"><img onclick="updateUser()" style="cursor:pointer;" src="';
                var htmlR = '" width="80px" height="80px"></div><div class="col-xs-7" id="userName" onclick="updateUser()" style="cursor:pointer;"><div class="text-lg ">'
                    + userData.userName + '</div><div class="text-center">'
                    + userData.nickName + '</div></div><div><ul class="nav navbar-nav" style="margin-left: 10%;"><li><a href="publish.html?userId=' + userId +'&phone='+ phone+'">发布</a></li>' +
                    '<li><a onclick="javascript:myLove(userId); return false;" href="#">收藏</a></li><li ><a onclick="javascript:myPush(userId); return false;" href="#" >我的帖子</a></li><li style="margin-left: 33%;">' +
                    '<div style="color: red;cursor:pointer;" onclick="quit()" >退出登陆</div></li></ul></div></div>'
                if (userData.avatar != null && userData.avatar != "") {
                    $("#user").append(
                        htmlL + urlapi + userData.phone + "/" + userData.avatar + htmlR
                    )
                } else {
                    $("#user").append(
                        htmlL + '../img/user.jpg"' + htmlR
                    )
                }
            },
            error: function () {

            }
        });
        findAll();
    });


    //判断有无图片
    function findUploadImg(String) {
        var fileList = new Array()
        var imgTag = '<img src=([\u4E00-\u9FA5A-Za-z0-9%_/.]*)/>'
        var result = String.match(imgTag)
        if (result == null) {
            fileList = null;
        } else {
            for (var i = 0; i < result.length; i++) {
                var imgName = result[1]
                fileList.push(imgName)
            }
        }
        return fileList
    }
</script>

</html>